package states;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.gui.AbstractComponent;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import dao.DbClass;

import entity.Player;

import ui.core.Button;
import ui.listener.ChangeGameStateListener;
import utils.ResourceManager;
import utils.SaveManager;

public class MainMenuState extends BasicGameState implements ComponentListener{

	public static final int ID = 1;
	private static StateBasedGame game;
	private static GameContainer container;
	private Button continueGameButton;
	private Button newGameButton;
	private Button quitGameButton;
	private Image background;
	private boolean showContinue = false;
	DbClass db = new DbClass();
	
	public int getID() {
		return ID;
	}

	
	public void init(GameContainer container, StateBasedGame game)
			throws SlickException{		
		MainMenuState.container = container;
		MainMenuState.game = game;
		background = ResourceManager.getImage("pokemonBackground");
//		bgImage = ResourceManager.getImage("pokemonBackground");
//		Player player = SaveManager.openGame(null);
		ResultSet rs = db.executeQuery("SELECT * FROM player_pokemons");
		try {
			if(rs.next()){
				showContinue = true;
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
//		if(player.getPokemon().getId() != 0){
//			showContinue = true;
//		}
		//Button creation
		newGameButton = new Button(container, ResourceManager.getImage("newGameButtonNormalImage"), 215, 200);
		//Seting mouse over image
		newGameButton.setMouseOverImage(ResourceManager.getImage("newGameButtonOverImage"));
		//adding listeners
		newGameButton.addListener(new ChangeGameStateListener(game,
				IntroState.ID));
		newGameButton.addListener(new ComponentListener() {			
			public void componentActivated(AbstractComponent comp) {								
				try {
					MainMenuState.game.getState(IntroState.ID).init(MainMenuState.container, MainMenuState.game);					
				} catch (SlickException e) {
					System.out.println("Impossivel reiniciar o metodo init do InGameState");
				}	
			}
		});

		continueGameButton = new Button(container, ResourceManager.getImage("continueGameButtonNormalImage"), 215, 250);
		continueGameButton.setMouseOverImage(ResourceManager.getImage("continueGameButtonOverImage"));
		continueGameButton.addListener(new ChangeGameStateListener(game,
				InGameState.ID));
		continueGameButton.addListener(new ComponentListener() {			
			@Override
			public void componentActivated(AbstractComponent source) {
				// TODO Auto-generated method stub
				try {
					MainMenuState.game.getState(InGameState.ID).init(MainMenuState.container, MainMenuState.game);						
				} catch (SlickException e) {
					System.out.println("Impossivel reiniciar o metodo init do InGameState");
				}
			}
		});		

		quitGameButton = new Button(container, ResourceManager.getImage("quitGameButtonNormalImage"), 215, 300);	
		quitGameButton.setMouseOverImage(ResourceManager.getImage("quitGameButtonOverImage"));
		quitGameButton.addListener(this);
	}

	
	public void update(GameContainer container, StateBasedGame game, int delta)
			throws SlickException {
		
	}

	
	public void render(GameContainer container, StateBasedGame game, Graphics g)
			throws SlickException {
		background.draw(0, 0, container.getWidth(), container.getHeight());		
		newGameButton.render(container, g);
		if(showContinue){
			continueGameButton.render(container, g);
		}
		quitGameButton.render(container, g);
	}


	@Override
	public void componentActivated(AbstractComponent source) {
		container.exit();
	}
}
